package com.bocheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocheng.auto.JSONAuthentication;
import com.bocheng.entity.TOrder;
import com.bocheng.entity.User;
import com.bocheng.mapper.ComMapper;
import com.bocheng.mapper.TOrderMapper;
import com.bocheng.mapper.UserMapper;
import com.bocheng.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bocheng
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/t-order")
public class TOrderController extends JSONAuthentication {
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ComMapper comMapper;
    @ApiOperation(value = "添加订单")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("comId") int  comId, @RequestParam("price") float price, @RequestParam("num") int  num,@RequestParam("userId") int userId) throws  Exception{
        TOrder order=new TOrder();
        order.setUserId(userId);
        order.setComId(comId);
        order.setPrice(price);
        order.setNum(num);
        User user=userMapper.selectById(order.getUserId());
        if(user.getBalance()>order.getPrice()){
            user.setBalance(user.getBalance()-order.getPrice());
            if(tOrderMapper.insert(order)>0&&userMapper.updateById(user)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("余额不足，请充值");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除订单")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=tOrderMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "发货订单")
    @PostMapping("/orderFh")
    public void orderFh(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id)throws Exception{
        TOrder order =tOrderMapper.selectById(id);
        order.setStatus("已发货");
        if(tOrderMapper.updateById(order)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "签收订单")
    @PostMapping("/orderQs")
    public void orderQs(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id)throws Exception{
        TOrder order =tOrderMapper.selectById(id);
        order.setStatus("已发货");
        if(tOrderMapper.updateById(order)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有订单通过用户id")
    @PostMapping("/findAllOrderByUSerId")
    public void findAllOrderByUSerId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<TOrder> shUserPage=new Page<>(current,size);
        Page<TOrder> shUserPages=tOrderMapper.selectPage(shUserPage,wrapper);
        List<TOrder> records = shUserPages.getRecords();
        int total=tOrderMapper.selectCount(wrapper);
        for(TOrder a:records){
            a.setUser(userMapper.selectById(a.getUserId()));
            a.setCom(comMapper.selectById(a.getComId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有订单")
    @PostMapping("/findAllOrder")
    public void findAllOrderByUSerId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam ("current") int current)throws  Exception{
        Page<TOrder> shUserPage=new Page<>(current,size);
        Page<TOrder> shUserPages=tOrderMapper.selectPage(shUserPage,null);
        List<TOrder> records = shUserPages.getRecords();
        int total=tOrderMapper.selectCount(null);
        for(TOrder a:records){
            a.setUser(userMapper.selectById(a.getUserId()));
            a.setCom(comMapper.selectById(a.getComId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

